﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System.Linq;

namespace DNA.Mvc.Data.Xml
{
    public static class PackageConverter
    {
        public static Package Convert(PackageDescriptor descriptor)
        {
            var pkg = new Package()
            {
                Name = descriptor.Name,
                Title = descriptor.Title,
                Description = descriptor.Description,
                Published = descriptor.Published,
                Version = descriptor.Version.Build,
                ReleaseNotes = descriptor.Version.Notes
            };

            if (!string.IsNullOrEmpty(descriptor.Author.Name))
            {
                var contact = new Contact()
                {
                    Name = descriptor.Author.Name,
                    Email = descriptor.Author.Email,
                    Organzation = descriptor.Author.Organization,
                    WebSite = descriptor.Author.Url
                };
                pkg.Authors.Add(contact);
            }

            pkg.Language = "en-US";

            if (descriptor.Licenses != null)
            {
                pkg.Licenses = (from lic in descriptor.Licenses
                                select new Res()
                                {
                                    Title = lic.Name,
                                    Url = lic.Url
                                }).ToList();

            }

            if (descriptor.Links != null)
            {
                pkg.Links = (from lic in descriptor.Links
                             select new Res()
                             {
                                 Title = lic.Title,
                                 Url = lic.NavigateUrl
                             }).ToList();
            }

            if (descriptor.Images != null)
            {
                foreach (var img in descriptor.Images)
                {
                    pkg.Images.Add(new ImageRes()
                    {
                        Title = img.Title,
                        Url = img.ImageUrl,
                        IsDefault = img.IsDefault
                    });
                }
            }
            return pkg;
        }

        public static PackageDescriptor Convert(Package package)
        {
            var pd = new PackageDescriptor()
            {
                Title = package.Title,
                Description = package.Description,
                Name = package.Name,
                Published = package.Published,
            };

            if (package.Authors.Count > 0)
            {
                var auth = package.Authors.First();
                pd.Author = new AuthorElement()
                {
                    Name = auth.Name,
                    Email = auth.Email,
                    Organization = auth.Organzation,
                    Url = auth.WebSite
                };
            }

            pd.Version = new VersionElement()
            {
                Build = package.Version
            };

            if (package.Licenses.Count > 0)
                pd.Licenses = package.Licenses.Select(lic => new LicenseElement() { Url = lic.Url, Name = lic.Title }).ToList();

            if (package.Links.Count > 0)
                pd.Links = package.Links.Select(link => new LinkElement() { NavigateUrl = link.Url, Title = link.Title }).ToList();

            if (package.Images.Count > 0)
                pd.Images = package.Images.Select(img => new ImageElement() { Title = img.Title, ImageUrl = img.Url, IsDefault = img.IsDefault }).ToList();
            return pd;
        }
    }
}
